package com.gpj.learn.rabbitmq.topic;

import com.gpj.learn.rabbitmq.common.CommonConstants;
import com.gpj.learn.rabbitmq.common.RabbitUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @className RoutingKeyProducer
 * @description
 *   TODO
 * @author GPJ
 * @date 2021/5/30 12:33
 * @version 1.0
 **/
@Slf4j
public class TopicProducer {
	public static void main(String[] args) throws IOException {
		log.info("------------- 开始发送消息 -------------");
		Channel channel = RabbitUtil.createChannel();

		// 声明交换机
		channel.exchangeDeclare(
				CommonConstants.RABBITMQ_EXCHANGE_TOPIC,
				BuiltinExchangeType.TOPIC,
				false, // 是否持久化
				false, // 是哦否自动删除
				false, // 是否内置,true-内置交换机,客户端线程无法直接发送消息到该交换机上
				null); // 额外信息

		channel.basicPublish(CommonConstants.RABBITMQ_EXCHANGE_TOPIC,
				"topic.key1.topicA", null, "topic message no.1 key1 topicA".getBytes());
		channel.basicPublish(CommonConstants.RABBITMQ_EXCHANGE_TOPIC,
				"topic.key1.topicB", null, "topic message no.1 key1 topicB".getBytes());
		channel.basicPublish(CommonConstants.RABBITMQ_EXCHANGE_TOPIC,
				"topic.key1.topicC", null, "topic message no.1 key1 topicC".getBytes());
		channel.basicPublish(CommonConstants.RABBITMQ_EXCHANGE_TOPIC,
				"topic.key2.topicA", null, "topic message no.1 key2 topicA".getBytes());
		channel.basicPublish(CommonConstants.RABBITMQ_EXCHANGE_TOPIC,
				"topic.key2.topicB", null, "topic message no.1 key2 topicB".getBytes());
		channel.basicPublish(CommonConstants.RABBITMQ_EXCHANGE_TOPIC,
				"topic.key2.topicC", null, "topic message no.1 key2 topicC".getBytes());
		RabbitUtil.close(channel);

		log.info("------------- 消息发送完成 -------------");
	}
}
